package com.star.common.utils;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import com.star.common.exception.CustomException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * TODO
 *
 * @author xiaoqiang
 * @version 1.0
 * @date 2020/8/8 17:09
 */
public class DateUtil {

    public static final String FULL_TIME_PATTERN = "yyyyMMddHHmmss";

    public static final String FULL_TIME_SPLIT_PATTERN = "yyyy-MM-dd HH:mm:ss";

    /**
     * 根据日期获取年龄
     *
     * @param birthDay
     * @return int
     * @author xiaoqiang
     * @date 2020/8/7 12:02
     */
    public static int getAge(Date birthDay) {
        // 获取当前系统时间
        Calendar cal = Calendar.getInstance();
        // 如果出生日期大于当前时间，则抛出异常
        if (cal.before(birthDay)) {
            throw new CustomException("出生日期异常");
        }
        // 取出系统当前时间的年、月、日部分
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        // 将日期设置为出生日期
        cal.setTime(birthDay);
        // 取出出生日期的年、月、日部分
        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
        // 当前年份与出生年份相减，初步计算年龄
        int age = yearNow - yearBirth;
        // 当前月份与出生日期的月份相比，如果月份小于出生月份，则年龄上减1，表示不满多少周岁
        if (monthNow <= monthBirth) {
            // 如果月份相等，在比较日期，如果当前日，小于出生日，也减1，表示不满多少周岁
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) {
                    age--;
                }
            } else {
                age--;
            }
        }
        return age;
    }

    public static String formatFullTime(LocalDateTime localDateTime) {
        return formatFullTime(localDateTime, FULL_TIME_PATTERN);
    }

    public static String formatFullTime(LocalDateTime localDateTime, String pattern) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        return localDateTime.format(dateTimeFormatter);
    }

    public static String formatCSTTime(String date, String format) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("EEEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
        Date d = sdf.parse(date);
        return DateUtil.getDateFormat(d, format);
    }

    private static String getDateFormat(Date date, String dateFormatType) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormatType);
        return simpleDateFormat.format(date);
    }
    public static String offsetDay(Date date,int offset){
        DateTime dateTime = cn.hutool.core.date.DateUtil.offsetDay(date, offset);
        return dateTime.toString(DatePattern.NORM_DATE_PATTERN);
    }


    /**
     * 今天
     *
     * @author xiaoqiang
     * @date 2020/11/30 15:17
     * @return java.lang.String
     */
    public static String today(Date date){
        return offsetDay(date,0);
    }

    /**
     * 昨天
     *
     * @author xiaoqiang
     * @date 2020/11/30 15:17
     * @return java.lang.String
     */
    public static String yesterday(Date date){
        return offsetDay(date,-1);
    }

    /**
     * 一周前
     *
     * @author xiaoqiang
     * @date 2020/11/30 15:17
     * @return java.lang.String
     */
    public static String week(Date date){
        return offsetDay(date,-7);
    }

    /**
     * 前一个月
     *
     * @author xiaoqiang
     * @date 2020/11/30 15:17
     * @return java.lang.String
     */
    public static String month(Date date){
        return offsetDay(date,-30);
    }

    /**
     * 本月第一天
     *
     * @author xiaoqiang
     * @date 2020/11/30 16:45
     * @return java.lang.String
     */
    public static String firstDayOfTheMonth(){
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
        return format.format(c.getTime());
    }

    /**
     * 本月最后一天
     *
     * @author xiaoqiang
     * @date 2020/11/30 16:45
     * @return java.lang.String
     */
    public static String  lastDayOfTheMonth(){
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        //获取当前月最后一天
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        return format.format(ca.getTime());
    }
    /**
     * 上月第一天
     *
     * @author xiaoqiang
     * @date 2020/11/30 16:45
     * @return java.lang.String
     */
    public static String getBeforeFirstMonthDate(){
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return format.format(calendar.getTime());
    }

    /**
     * 上月最后一天
     *
     * @author xiaoqiang
     * @date 2020/11/30 16:45
     * @return java.lang.String
     */
    public static String getBeforeLastMonthDate() {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return sf.format(calendar.getTime());
    }
    public static void main(String[] args){
        System.out.println("本月第一天："+firstDayOfTheMonth());
        System.out.println("本月最后一天："+lastDayOfTheMonth());
        System.out.println("上月第一天："+getBeforeFirstMonthDate());
        System.out.println("上月最后一天："+getBeforeLastMonthDate());
    }

}
